﻿namespace BeatEmUp.World.Drawable
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using BeatEmUp.Menu;
    using BeatEmUp.Characters.Enemies;
    using BeatEmUp.HUD;
    using BeatEmUp.Items.Weapons;
    using BeatEmUp.Enums;
    using Microsoft.Xna.Framework.Content;

    class StaticDrawable: IDrawable
    {
        private string textureName;
        private Texture2D texture;

        public Rectangle Source
        {
            private set;
            get;
        }

        public Vector2 Size
        {
            private set;
            get;
        }

        public StaticDrawable(string textureName)
        {
            this.textureName = textureName;
        }

        public static StaticDrawable CreateInSize(string textureName, Vector2 size)
        {
            StaticDrawable drawable = new StaticDrawable(textureName);
            drawable.Size = size;

            return drawable;
        }

        public static StaticDrawable CreateUsingSegment(string textureName, Rectangle source)
        {
            StaticDrawable drawable = new StaticDrawable(textureName);
            drawable.Source = source;

            return drawable;
        }

        public static StaticDrawable CreateInSizeUsingSegment(string textureName, Vector2 size, Rectangle source)
        {
            StaticDrawable drawable = new StaticDrawable(textureName);
            drawable.Size = size;
            drawable.Source = source;

            return drawable;
        }

        public Drawable.IDrawable Clone()
        {
            return (Drawable.IDrawable)this.MemberwiseClone();
        }

        public void LoadContent(ContentManager contentManager)
        {
            this.texture = contentManager.Load<Texture2D>(this.textureName);

            if (0 == this.Size.Length())
            {
                this.Size = new Vector2((float) this.texture.Width, (float) this.texture.Height);
            }
        }

        public void Update(GameTime gameTime) {}

        public void Draw(GameManager gameManager, GameTime gameTime, Vector2 position)
        {
            Rectangle target = this.getRect(position, this.Size);

            if (this.Source.IsEmpty)
                GameManager.customResolution.Draw(this.texture, target, Color.White);
            else
                GameManager.customResolution.Draw(this.texture, target, this.Source, Color.White);
        }

        protected Rectangle getRect(Vector2 position, Vector2 dimensions)
        {
            Rectangle target = new Rectangle();

            target.X = (int) position.X;
            target.Y = (int) position.Y;

            target.Width =  (int) dimensions.X;
            target.Height = (int) dimensions.Y;

            return target;
        }
    }
}
